package com.yang.blog.util;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import java.beans.PropertyDescriptor;
import java.util.ArrayList;
import java.util.List;

public class MyBeanUtils {
//能获取当前对象所有属性值为空的一组属性，最终返回的是String[]属性名称的数组
    public static String[] getNullPropertyNames(Object source){
        BeanWrapper beanWrapper = new BeanWrapperImpl(source);
        PropertyDescriptor[] pds = beanWrapper.getPropertyDescriptors();
        List<String> nullPropertyNames = new ArrayList<>();
        for (PropertyDescriptor pd:pds){
            String propertyName =pd.getName();
        if (beanWrapper.getPropertyValue(propertyName) ==null){
            nullPropertyNames.add(propertyName);
             }
        }
        return nullPropertyNames.toArray(new String[nullPropertyNames.size()]);
    }
}
